package com.woniuxy.oa.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.oa.entity.Holiday;
import com.woniuxy.oa.entity.Reimbursement;
import com.woniuxy.oa.exception.ApprovalException;
import com.woniuxy.oa.exception.HolidayException;
import com.woniuxy.oa.exception.ReimbursementException;
import com.woniuxy.oa.mapper.ReimbursementMapper;
import com.woniuxy.oa.service.ReimbursementService;

@Service
public class ReimbursementServiceImpl implements ReimbursementService{

	@Autowired
	private ReimbursementMapper reimbursementMapper;
	
	public void setReimbursementMapper(ReimbursementMapper reimbursementMapper) {
		this.reimbursementMapper=reimbursementMapper;
	}

	//插入一条报销申请的方法
	@Override
	public Integer addReimbursementGetId(Integer a_id, Reimbursement reimbursement) {
		try {
			reimbursementMapper.insertReimbursementGetId(a_id,reimbursement);
			if(reimbursement.getRb_id()>0) {
				return reimbursement.getRb_id();
			}
			throw new ReimbursementException("插入申请失败");
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReimbursementException(e);
		}
	}

	@Override
	//根据a_id找发起人id
	public Integer getIdByAid(Integer a_id) {
		try {
			Integer e_id = reimbursementMapper.selectE_idByA_id(a_id);
			if(e_id>0) {
				return e_id;
			}
			throw new ApprovalException("没有根据aid"+a_id+"找到发起人id");
		} catch (Exception e) {
			throw new ApprovalException(e);
		}
	}

	@Override
	public void deleteReimbursementByAid(Integer a_id) {
		try {
			reimbursementMapper.updateReimbursementFlagByAid(a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReimbursementException(e);
		}
	}

	//根据报销主键查报销
	@Override
	public Reimbursement selectReimbursementByRbid(Integer rb_id) {
		try {
			Reimbursement reimbursement = reimbursementMapper.selectReimbursementByRbid(rb_id);
			return reimbursement ; 
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReimbursementException(e);
		}
	}

	//修改报销信息
	@Override
	public void updateHoliday(Reimbursement reimbursement, Integer rb_id) {
		try {
			reimbursementMapper.updateReimbursement(reimbursement,rb_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReimbursementException(e);
		}
	}

	//修改审批关系的方法
	@Override
	public void updateApprovalAeid(Integer a_eid, Integer a_id) {
		try {
			reimbursementMapper.updateApprovalAeid(a_eid, a_id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReimbursementException(e);
		}
	}
}
